package com.suanfa.string;

import java.util.HashMap;
import java.util.Map;

/**
 * 提供一个key和message, key的字母顺序和abc...简历对应关系，解密message
 */
public class DecrptString {

    public static void main(String[] args) {
        String key = "the quick brown fox jumps over the lazy dog";
        String value = "vkbs bs t suepuv";
        System.out.println(decodeMessage(key, value));
    }

    public static String decodeMessage(String key, String message) {

        Map<String, String> map = new HashMap();

        char[] chs = key.toCharArray();

        int base = 97;

        for(char ch : chs){
            if(ch != ' ' && !map.containsKey(ch+"")){
                char v = (char)base;
                map.put(ch+"", v+"");
                base ++;
            }
        }

        char[] me = message.toCharArray();
        StringBuilder sb = new StringBuilder();
        for(char ch : me){
            if(ch != ' ') {
                sb.append(map.get(ch + ""));
            }else {
                sb.append(ch + "");
            }
        }

        return sb.toString();

    }
}
